﻿#if UNITY_IOS
using System.Runtime.InteropServices;
#endif
using UnityEngine;

namespace BToolkit
{
    public class WeiXinJumpMP : MonoBehaviour
    {
        static WeiXinJumpMP _instance;
        public static WeiXinJumpMP Instance
        {
            get
            {
                if (!_instance)
                {
                    _instance = GameObject.FindObjectOfType<WeiXinJumpMP>();
                    if (!_instance)
                    {
                        GameObject go = new GameObject("WeiXinJumpMP");
                        DontDestroyOnLoad(go);
                        _instance = go.AddComponent<WeiXinJumpMP>();
                    }
                }
                return _instance;
            }
        }

        /// <summary>
        /// 跳转到微信小程序
        /// <param name="appId">appId:开发者appId</param>
        /// <param name="targetMPOriginId">targetMPOriginId:所跳转的目标小程序原始ID</param>
        /// <param name="mpPath">mpPath:拉起小程序页面的可带参路径，不填默认拉起小程序首页，对于小游戏，可以只传入query部分，来实现传参效果，如：传入 "?foo=bar"</param>
        /// <param name="type">type:0正式 1测试 2体验</param>
        /// </summary>
        public void JumpToMP(string appId, string targetMPOriginId, string mpPath, int type)
        {
            if (Application.platform == RuntimePlatform.Android)
            {
                Debuger.LogError("调用安卓原生");
                AndroidUtils.CallAndroidStaticFunction("cn.btoolkit.weixinapi.AppJumpMiniProgram", "jump", appId, targetMPOriginId, mpPath, type);
            }
            else if (Application.platform == RuntimePlatform.IPhonePlayer)
            {
#if UNITY_IOS
                _Share(appId, targetMPOriginId, mpPath, type);
#endif
            }
        }


#if UNITY_IOS
        [DllImport("__Internal")]
        static extern void _JumpToMP(string appId, string mpOriginId, string mpPath, int type);
#endif

    }
}